package com.edu.javaweb.controller;

import com.edu.javaweb.pojo.*;
import com.edu.javaweb.service.*;
import com.edu.javaweb.vo.CGAdminVo;
import com.edu.javaweb.vo.Rule;
import com.edu.javaweb.vo.StudentRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private StuClassService stuClassService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private HolidayService holidayService;
    @Autowired
    private HolidayInformationService holidayInformationService;
    @Autowired
    private StudentRecordService studentRecordService;

    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin/showAllStuRecord")
    @ResponseBody
    public List<StudentRecordVo> showAllStuRecord(Rule rule){
        System.out.println(rule);
        return studentRecordService.findByStuRecordGroup(rule);
    }
    @RequestMapping("/admin/showNowStuRecord")
    @ResponseBody
    public List<StudentRecordVo> showNowStuRecord(Rule rule,HttpSession session){
        System.out.println(rule);
        HolidayInformation holidayInformation=(HolidayInformation) session.getAttribute("holidayInformation");
        rule.setHIId(holidayInformation.getId());
        return studentRecordService.findByStuRecordGroup(rule);
    }

    @RequestMapping("/admin/cgAdminPassword")
    @ResponseBody
    public void cgAdminPassword(CGAdminVo cgAdminVo){
        Admin admin = adminService.getAdmin(cgAdminVo.getAccount());
        if (admin.getPassword().equals(cgAdminVo.getOldPassword())){
            admin.setPassword(cgAdminVo.getNewPassword());
            adminService.cgAdminPassword(admin);
        }
    }





    //假期信息管理
    @RequestMapping("/admin/sendToRecord/{id}")
    @ResponseBody
    public void sendToRecord(@PathVariable Integer id, HttpSession session){
        HolidayInformation holidayInformation=holidayInformationService.findById(id);
        session.setAttribute("holidayInformation",holidayInformation);
    }
    @RequestMapping("/admin/delHolidayInformation/{id}")
    @ResponseBody
    public void delHolidayInformation(@PathVariable Integer id){
        holidayInformationService.delById(id);
    }
    @RequestMapping("/admin/insOrUpHolidayInformation")
    @ResponseBody
    public void insHolidayInformation(HolidayInformation holidayInformation){
        if (holidayInformation.getId()==0){
            System.out.println("执行添加操作");
            holidayInformationService.insHolidayInformation(holidayInformation);
        }else {
            System.out.println("执行更新操作");
            holidayInformationService.upHolidayInformation(holidayInformation);
        }
    }
    @RequestMapping("/admin/showHolidayInformation")
    @ResponseBody
    public List<HolidayInformation> showHolidayInformation(){
        return holidayInformationService.findAll();
    }






    //假期管理
    @RequestMapping("/admin/delHoliday/{id}")
    @ResponseBody
    public void delHoliday(@PathVariable Integer id){
        holidayService.delById(id);
    }
    @RequestMapping("/admin/insOrUpHoliday")
    @ResponseBody
    public void insHoliday(Holiday holiday){
        if (holiday.getId()==0){
            System.out.println("执行添加操作");
            holidayService.insHoliday(holiday);
        }else {
            System.out.println("执行更新操作");
            holidayService.upHoliday(holiday);
        }
    }
    @RequestMapping("/admin/showHoliday")
    @ResponseBody
    public List<Holiday> showHoliday(){
        return holidayService.findAll();
    }







    //学生管理
    @RequestMapping("/admin/delStudent/{id}")
    @ResponseBody
    public void delStudent(@PathVariable Integer id){
        studentService.delById(id);
    }
    @RequestMapping("/admin/insOrUpStudent")
    @ResponseBody
    public void insStudent(Student student){
        if (student.getId()==0){
            System.out.println("执行添加操作");
            studentService.insStudent(student);
        }else {
            System.out.println("执行更新操作");
            studentService.upStudent(student);
        }
    }
    @RequestMapping("/admin/showStudent")
    @ResponseBody
    public List<Student> showStudent(){
        return studentService.findAll();
    }


    //班级管理
    @RequestMapping("/admin/delClass/{id}")
    @ResponseBody
    public void delClass(@PathVariable Integer id){
        stuClassService.delById(id);
    }
    @RequestMapping("/admin/insOrUpClass")
    @ResponseBody
    public String insClass(StuClass stuClass){
        if (stuClass.getId()==0){
            System.out.println("执行添加操作");
            stuClassService.insClass(stuClass);
        }else {
            System.out.println("执行更新操作");
            stuClassService.upClass(stuClass);
        }
        return "admin";
    }
    @RequestMapping("/admin/showClass")
    @ResponseBody
    public List<StuClass> showClass(){
        return stuClassService.findAll();
    }

}
